<?php

namespace App\Console;

use App\Enums\BorrowStatus;
use App\Models\BorrowRecord;
use App\Settings\Borrow;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Mail;

class BorrowOverDue
{
   public function __invoke(Borrow $settings)
   {
        BorrowRecord::where('returned',0)
            ->where('borrow_date','<',now()->subDays($settings->borrow_days))
            ->orWhere('deadline','<',now())
            ->chunkById(100, function (Collection $records) {
                foreach ($records as $record) {
                    $record->returned = BorrowStatus::STATUS_OVERDUE;
                    $record->save();
                    try {
                        Mail::to($record->member->email)->queue(new \App\Mail\BorrowOvered($record));
                    } catch (\Throwable $e) {
                        print_r($e->getMessage());
                    }

                }
            });
   }
}
